package com.scsystem.xysc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scsystem.xysc.aop.LogOperations;
import com.scsystem.xysc.pojo.Comment;
import com.scsystem.xysc.pojo.Order;
import com.scsystem.xysc.pojo.vo.ReturnResult;
import com.scsystem.xysc.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/comment")
@Tag(name = "评论接口")
public class CommentController {

    @Resource
    private CommentService commentService;

    @PostMapping("/postComments")
    @Parameters({
            @Parameter(name = "uid", description = "用户id"),
            @Parameter(name = "pid", description = "商品id"),
            @Parameter(name = "content", description = "评价内容"),
            @Parameter(name = "rating", description = "评分"),
    })
    @Operation(summary = "发表评论")
    @LogOperations(module = "评论", operation = "发表商品评论")
    public ReturnResult<Boolean> postComments(Long uid, Long pid, String content, Integer rating) {
        return commentService.addComment(uid, pid, content, rating);
    }


    @GetMapping("/getCommentsByProductId")
    @Parameters({
            @Parameter(name = "pid", description = "商品id"),
    })
    @Operation(summary = "通过商品id获取评论")
    public ReturnResult<List<Comment>> getCommentsByProductId(Long pid) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", pid);
        List<Comment> list = commentService.list(queryWrapper);
        return new ReturnResult<>(200, "查询成功", list);
    }

    @GetMapping("/getCommentsByUserId")
    @Parameters({
            @Parameter(name = "uid", description = "用户id"),
    })
    @Operation(summary = "通过用户id获取评论")
    public ReturnResult<List<Comment>> getCommentsByUserId(Long uid) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", uid);
        List<Comment> list = commentService.list(queryWrapper);
        return new ReturnResult<>(200, "查询成功", list);
    }

    @GetMapping("/like")
    @Parameters({
            @Parameter(name = "cid", description = "评论id"),
    })
    @Operation(summary = "点赞")
    public ReturnResult<Boolean> like(Long cid) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id", cid);
        Comment one = commentService.getOne(queryWrapper);
        one.setUserLike(one.getUserLike() + 1);
        boolean b = commentService.updateById(one);
        return b ? new ReturnResult<>(200, "点赞成功", true) : new ReturnResult<>(400, "点赞失败", false);
    }

}
